/**
 * Created by liubin on 2017/6/13.
 */



var DtjTableEndLayer = GameCCSBase.extend({
    Panel_players : null,

    mRoundEndInfo : null,

    mPlayerCards : null,

    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/DtjTableEnd.json");
        this.Panel_players = [];

        this.mPlayerCards = [[], [], [], []];

        //确定
        mo.seekWidgetByName(this.ccsNode, "Button_ok").addTouchEventListener(function (sender, type) {
            if (mo.gameHelper.getIsTouchEnd(type)) {
                if (mo.dtjTable.mPlayerType == 0){
                    //录像x
                    this.setVisible(false);
                    mo.dtjTable.mDtjTableReplayLayer.onPlayEnd();
                }else{
                    if (mo.dtjTable.mDtjTableRoomEndLayer == null){
                        // mo.dtjTable.reset();
                        mo.gameMsgManager.sendGameReadyRequestdtj();
                    }else{
                        //打完了 显示大结算
                        mo.dtjTable.mDtjTableRoomEndLayer.setVisible(true);
                        this.setVisible(false);
                    }
                }
            }
        }, this);

        this.loadUI();

        this.setVisible(false);
    },

    loadUI : function(){
        for (var i = 0; i < 4; i++){
            var Panel_player = mo.seekWidgetByName(this.ccsNode, "Panel_player" + i);
            this.Panel_players.push(Panel_player);

            var Panel_cardPos = mo.seekWidgetByName(Panel_player, "Panel_cardPos");
            for (var j=0; j<4; j++){
                var card = new DtjCard();
                card.setCardNum(0);
                card.setScale(0.4);
                Panel_cardPos.addChild(card);

                if(j < 2){
                    card.setRotation(90);
                    card.setPosition(cc.p(40 + 25, -10 + 25 * j));
                }else{
                    card.setPosition(cc.p((3 - j)*25, 0));
                }


                this.mPlayerCards[i].push(card);
            }
        }
    },

    setVisible : function(isVisible){
        this.unschedule(this.updateEndInfo);
        this._super(isVisible);
    },

    setEndInfo : function(roundEndInfo, delayTime){
        this.mRoundEndInfo = roundEndInfo;


        if(mo.dtjTable.mDtjTableRoomEndLayer == null){
            this.scheduleOnce(function(){
                this.updateEndInfo();
            }, delayTime);

            var autoReadyDelayTime = Math.random() * 2 + 1;
            this.scheduleOnce(function(){
                mo.gameMsgManager.sendGameReadyRequestdtj();
            }, delayTime + autoReadyDelayTime);
        }else{
            mo.dtjTable.mDtjTableRoomEndLayer.setVisible(true);
            this.setVisible(false);
        }

    },

    updateEndInfo : function(){
        this.setVisible(true);
        mo.seekWidgetByName(this.ccsNode, "Panel_center").setScale(0);
        mo.seekWidgetByName(this.ccsNode, "Panel_center").runAction(cc.sequence(cc.scaleTo(0.3, 1.2), cc.scaleTo(0.1, 1.0)));
        var roundEndInfo = this.mRoundEndInfo;

        //自己的输赢
        var mySeatUser = roundEndInfo["SeatUser" + roundEndInfo.MySeat];
        var myids = mySeatUser.LostWin.ItemTypes;
        var mynums = mySeatUser.LostWin.ItemNums;
        var myallfen = mo.gameUserInfo.getItemNum(myids, mynums, 0);
        var myWin = myallfen >= 0;
        mo.gameHelper.loadTextureImage(mo.seekWidgetByName(this.ccsNode, "Image_winBg"), myWin ? "res/ccs/dtj/end/img_win.png" : "res/ccs/dtj/end/img_lose.png");
        mo.soundHelper.playDtjWin(myWin);


        var seatUsers = [];
        for (var seat = 0; seat < 4; seat++){
            var logicSeat = mo.dtjTableStatus.getLogicForView(seat, roundEndInfo.MySeat - 1);
            var seatUser = roundEndInfo["SeatUser" + (logicSeat + 1)];
            if(seatUser != null){
                seatUsers.push(seatUser);
            }
        }

        var handCardLength = 4;
        if(mo.dtjTable.mGameConfig.paijiu == 1){
            handCardLength = 2;
        }

        for (var seat = 0; seat < 4; seat++){
            var seatUser = seatUsers[seat];

            //
            var nickStr = "";
            var scoreStr = "";

            var Text_nick = mo.seekWidgetByName(this.Panel_players[seat], "Text_nick");
            var Image_head = mo.seekWidgetByName(this.Panel_players[seat], "Image_head");
            var Fnt_score = mo.seekWidgetByName(this.Panel_players[seat], "Fnt_score");

            if(seatUser != null){
                this.Panel_players[seat].setVisible(true);
                nickStr = seatUser.user_info.nickname;

                var allfen = 0;
                var ids = seatUser.LostWin.ItemTypes;
                var nums = seatUser.LostWin.ItemNums;
                allfen = mo.gameUserInfo.getItemNum(ids, nums, 0);
                if (allfen >= 0){
                    Fnt_score.setFntFile("res/ccs/dtj/end/bigWinFnt.fnt");
                    allfen = "+" + allfen;
                }else {
                    Fnt_score.setFntFile("res/ccs/dtj/end/bigLoseFnt.fnt");
                }
                scoreStr = allfen;

                Image_head.setVisible(true);
                mo.gameHelper.loadImageForUrl(Image_head, seatUser.user_info.faceUrl);


                var handCards = seatUser.HandCards;

                for (var i=0; i<4; i++){
                    var card = this.mPlayerCards[seat][i];
                    if(i < handCardLength && handCardLength == 2){
                        card.setVisible(false);
                    }else{
                        card.setVisible(true);
                        card.setCardNum(handCards[i]);
                        card.setCardSprite(card.mCardNum);
                        // card.setSpriteFrame("card"+card.mCardNum+".png");
                    }
                }

                var Panel_cardPos = mo.seekWidgetByName(this.Panel_players[seat], "Panel_cardPos");

                var cardTypeImg1 = mo.seekWidgetByName(Panel_cardPos, "Image_cardType1");
                var cardTypeImg2 = mo.seekWidgetByName(Panel_cardPos, "Image_cardType2");

                if(handCardLength == 2){
                    cardTypeImg2.setVisible(false);
                }

                var bigCardTypeIndx = mo.dtjCardHelper.getCardGroup(handCards[0], handCards[1]);
                var smallCardTypeIndx = mo.dtjCardHelper.getCardGroup(handCards[2], handCards[3]);

                mo.gameHelper.loadTextureImage(cardTypeImg1, "cardType" + (smallCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);
                mo.gameHelper.loadTextureImage(cardTypeImg2, "cardType" + (bigCardTypeIndx + 1) + ".png", ccui.Widget.PLIST_TEXTURE);

            }else{
                this.Panel_players[seat].setVisible(false);
                nickStr = "[空座位]";
                scoreStr = "0";

                Image_head.setVisible(false);
            }

            //nick
            mo.gameHelper.setTextString(Text_nick, mo.gameHelper.getUserNickFilterForCount(nickStr, 3));

            //杠分
            Fnt_score.setString(scoreStr);
        }
    }
});






